﻿/*----------------------------------------------------------------
//  <copyright file="EndpointExtensions.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-09-25 17:04</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Authorization
{
    #region "<see cref="RouteEndpoint"/> 扩展方法"
    /// <summary>
    /// <see cref="RouteEndpoint"/> 扩展方法
    /// </summary>
    public static class EndpointExtensions
    {
        #region "扩展方法"
        #region "获取正在执行的Action相关功能信息"
        /// <summary>
        /// 获取正在执行的Action相关功能信息
        /// </summary>
        /// <param name="endpoint"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static IFunction GetExecuteFunction(this RouteEndpoint endpoint, HttpContext context)
        {
            IServiceProvider provider = context.RequestServices;
            ScopedDictionary dict = provider.GetService<ScopedDictionary>();

            //TODO:这玩意儿貌似不能获取最新的数据，注释后从功能缓存中获取
            //if (dict.Function != null)
            //{
            //    return dict.Function;
            //}

            string area = endpoint.GetAreaName(),
                controller = endpoint.GetControllerName(),
                action = endpoint.GetActionName();
            IFunctionHandler functionHandler = provider.GetService<IFunctionHandler>();
            if (functionHandler == null)
            {
                //throw new MicroCloudException("获取正在执行的功能时 IFunctionHandler 无法解析");
                return null;
            }

            IFunction function = functionHandler.GetFunction(area, controller, action);
            if (function != null)
            {
                dict.Function = function;
            }

            return function;
        }
        #endregion
        #region "获取Area名"
        /// <summary>
        /// 获取Area名
        /// </summary>
        /// <param name="endpoint"></param>
        /// <returns></returns>
        public static string GetAreaName(this RouteEndpoint endpoint)
        {
            string area = null;
            if (endpoint.RoutePattern.Defaults.TryGetValue("area", out object value))
            {
                area = (string)value;
                if (area.IsNullOrWhiteSpace())
                {
                    area = null;
                }
            }

            return area;
        }
        #endregion
        #region "获取Controller名"
        /// <summary>
        /// 获取Controller名
        /// </summary>
        /// <param name="endpoint"></param>
        /// <returns></returns>
        public static string GetControllerName(this RouteEndpoint endpoint)
        {
            // return endpoint.RoutePattern.Defaults["controller"].ToString();

            string controller = null;
            if (endpoint.RoutePattern.Defaults.TryGetValue("controller", out object value))
            {
                controller = (string)value;
                if (controller.IsNullOrWhiteSpace())
                {
                    controller = null;
                }
            }
            return controller;
        }
        #endregion
        #region "获取Action名"
        /// <summary>
        /// 获取Action名
        /// </summary>
        /// <param name="endpoint"></param>
        /// <returns></returns>
        public static string GetActionName(this RouteEndpoint endpoint)
        {
            // return endpoint.RoutePattern.Defaults["action"].ToString();

            string action = null;
            if (endpoint.RoutePattern.Defaults.TryGetValue("action", out object value))
            {
                action = (string)value;
                if (action.IsNullOrWhiteSpace())
                {
                    action = null;
                }
            }
            return action;
        }
        #endregion

        #endregion

    }
    #endregion

}
